home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 1 / 001.d81 / cows and bulls (.txt) < prev    next >
Commodore BASIC  |  2022-08-26  |  7KB  |  256 lines

  1. 5 rem *** cows and bulls ***
  2. 10 rem
  3. 100 rem---- documentation module ------
  4. 110 rem
  5. 120 rem   r$ = general purpose variable
  6. 130 rem   np = number of players
  7. 140 rem   pn$(10)close= players names
  8. 150 rem   sn (4) = four digits for      155 rem            secret number
  9. 160 rem   tn = turn number
  10. 170 rem   gs$ = string for guesses
  11. 180 rem   gsclose(4) = four digit guess
  12. 190 rem   bu = bulls
  13. 200 rem   co = cows
  14. 210 rem   ag$ = another game switch
  15. 220 rem   wn$ = win switch
  16. 230 rem   i = general index
  17. 240 rem   j = general index
  18. 250 rem
  19. 300 rem---- housekeeping module -------
  20. 310 rem
  21. 320 dim pn$ (10)
  22. 330 dim sn  (04)
  23. 340 dim gs  (04)
  24. 345 sp$="                                       "
  25. 350 rem
  26. 500 rem---- executive module ----------
  27. 510 rem
  28. 515 gosub 9000
  29. 520 gosub 1000
  30. 521 rem  ... game instructions ...
  31. 530 gosub 2000
  32. 531 rem  ... get player's names...
  33. 540 gosub 3000
  34. 541 rem  ... play a game       ...
  35. 550 if ag$<>"y" then 560
  36. 555 print"[147]               new game[146]":for i = 1 to 3000: next i
  37. 557 goto 540
  38. 558 poke53281,0:print"[144][147]load"chr$(34);"hello";chr$(34)",8":print"run"
  39. 559 poke 631,13:poke632,13:poke198,2:poke65535,ix(k,1)
  40. 560 goto 63000
  41. 1000 rem---- game instructions --------
  42. 1010 rem
  43. 1020 poke 53281,7:poke 53280,6:print"[144][147]":poke657,128
  44. 1025 printchr$(14)
  45. 1030 print"              ";
  46. 1040 print"[195]ows & [194]ulls"
  47. 1050 print"";
  48. 1060 input "[196]o you need instructions?[160](y or n) "; r$
  49. 1070 if r$= "n" then 1990
  50. 1080 print"[147]"
  51. 1090 print"[195]ows & [194]ulls is designed to stimulate"
  52. 1100 print"your deductive powers."
  53. 1110 print:print"[212]he object of [195]ows & [194]ulls is to guess"
  54. 1120 print"a secret number known only to your":print"[195]ommodore."
  55. 1125 print:print"[217]our [195]ommodore generates the secret"
  56. 1126 print"number at random according to the"
  57. 1127 print"following restrictions:"
  58. 1130 print:print" 1. [212]he number has four digits"
  59. 1131 print:print" 2. [206]o digit may be repeated"
  60. 1135 print:print"[214]alid examples are:"
  61. 1136 print:print"      1234":print"      0526 (may start with zero)"
  62. 1137 print"      9870"
  63. 1140 gosub9000:print:print:input"            ** hit return **";r$
  64. 1145 print"[147]":print"[208]lay goes like this---"
  65. 1146 print:print"  1. [217]ou make a guess as to what you"
  66. 1147 print"     think the secret number is."
  67. 1148 print:print"  2. [217]our [195]ommodore evaluates your guess";
  68. 1149 print"     and responds with an indication"
  69. 1150 print"     of how good your guess was."
  70. 1151 print:print"  3. [217]ou record your guess and the"
  71. 1152 print"     response to it."
  72. 1153 print:print"  4. [212]hen you formulate another guess"
  73. 1154 print"     based on previous guesses and"
  74. 1155 print"     responses."
  75. 1156 print:print"  5. [212]he first person to guess the"
  76. 1157 print"     [195]ommodore's secret number wins.":print:print
  77. 1160 gosub9000:input"            ** hit return **";r$
  78. 1165 print"[147]":print"[212]his is show the commodore evalutes"
  79. 1166 print"your guess---":print:print" 1. [198]or each digit that you have in the"
  80. 1167 print"    correct position you are awarded":print"    one 'bull'."
  81. 1170 print:print" 2. [198]or a digit that is in the secret"
  82. 1171 print"    number but not in thte correct"
  83. 1172 print"    position you are awarded one 'cow'."
  84. 1175 print:print" 3. [196]igits that do not appear in the"
  85. 1176 print"    number at all generate no response.":gosub9000
  86. 1180 print"            ";:input"** hit return **";r$
  87. 1185 print"[147]":print"[193]ssume the secret number is:"
  88. 1186 printtab(13)"7032":print:print"then these are examples of guesses and"
  89. 1187 print"responses.":print
  90. 1190 printtab(13)"1243    2 cows"
  91. 1191 printtab(13)"2743    4 cows"
  92. 1192 printtab(13)"8039    2 bulls"
  93. 1193 printtab(13)"8079    1 bull & 1 cow"
  94. 1194 printtab(13)"7032    4 bulls (win)":gosub9000
  95. 1195 print"":input"            ** hit return **";r$
  96. 1200 print"[147]":print"        --- [200][201][206][212] ---"
  97. 1201 print:print"[205]ake your first two guesses"
  98. 1202 print:print"        1234    and    5678":print
  99. 1205 print"[212]hen if you have four responses from"
  100. 1206 print"your first two guesses,"
  101. 1207 print"    you will know 9 & 0 are not in"
  102. 1208 print"   the secret number."
  103. 1210 print:print"[201]f you have two responses from your"
  104. 1211 print"first two guesses,"
  105. 1212 print"   you will know 9 & 0 are in the"
  106. 1213 print"   secret number."
  107. 1215 print:print"[215]hat do you know if you have three"
  108. 1216 print"responses from the first two guesses?":print
  109. 1220 gosub9000:print"         ... [199][207][207][196][160][204][213][195][203] ..."
  110. 1221 print"            ** hit return **";
  111. 1222 input"";r$
  112. 1990 return
  113. 2000 rem---- get player's names -------
  114. 2010 np = 0
  115. 2020 print"[147]"
  116. 2030 np = np + 1
  117. 2040 print"[197]nter the name of player #[160]";np
  118. 2045 gosub9000
  119. 2050 print:input"[160]";pn$(np)
  120. 2060 print"[147]"
  121. 2070 input"[193]ny more players? (y or n) ";r$
  122. 2080 if r$= "y" then 2020
  123. 2090 return
  124. 3000 rem---- play a game --------------
  125. 3010 rem
  126. 3015 gosub9000
  127. 3020 gosub 4000
  128. 3021 rem ... generate secret number...
  129. 3030 gosub 5000
  130. 3031 rem ... present a turn
  131. 3040 if wn$ = "n" then 3030
  132. 3041 rem no win???
  133. 3050 if tn <>np then 3030
  134. 3060 return
  135. 4000 rem---- generate secret number ---
  136. 4010 rem
  137. 4020 i = 0
  138. 4030 i = i + 1
  139. 4040 if i > 4 then 4110
  140. 4050 sn (i) = int (rnd (1)* 10)
  141. 4060 j = 0
  142. 4070 j = j + 1
  143. 4080 if j = i then 4030
  144. 4090 if sn (i) = sn (j) then 4050
  145. 4100 goto 4070
  146. 4110 wn$ = "n"
  147. 4111 rem wn$close= win switch
  148. 4120 return
  149. 5000 rem---- present a turn -----------
  150. 5010 rem
  151. 5020 tn = tn + 1
  152. 5030 if tn > np then tn = 1
  153. 5040 print"[147]"
  154. 5050 print"[201]t is " pn$(tn) "'s turn"
  155. 5060 print""
  156. 5070 print"[207]ther players don't look"
  157. 5080 print""
  158. 5090 print"[208]ress a key "pn$(tn)"."
  159. 5100 getk$:if k$=""then 5100
  160. 5105 gosub9000
  161. 5110 gosub 6000
  162. 5111 rem  ... get a guess ...
  163. 5120 gosub 7000
  164. 5121 rem  ... evaluate guess ...
  165. 5130 gosub 8000
  166. 5131 rem  ... display guess result...
  167. 5140 return
  168. 6000 rem---- get a guess --------------
  169. 6010 rem
  170. 6020 print"[147]"
  171. 6030 printtab(10)"[207][203], "pn$(tn)
  172. 6040 print"":print"[197]nter your guess, 4 digits, no repeats"
  173. 6050 print"                 ----"
  174. 6055 print"               ";
  175. 6059 gs$=""
  176. 6060 input"";gs$
  177. 6070 if len (gs$) <> 4 then 6180
  178. 6072 if left$(gs$,1)="e"  then 6180
  179. 6074 if left$(gs$,1)="+"then 6180
  180. 6080 if val (gs$)<123 then 6180
  181. 6081 if val (gs$)> 9876 then 6180
  182. 6090 i = 0
  183. 6100 i = i + 1
  184. 6110 if i > 4 then 6300
  185. 6120 gs (i) = val (mid$(gs$,i,1))
  186. 6130 j = 0
  187. 6140 j = j + 1
  188. 6150 if j = i then 6100
  189. 6160 if gs (i) = gs (j) then 6180
  190. 6170 goto 6140
  191. 6180 print""
  192. 6190 print"                                       "
  193. 6200 print"[145][145]               ";
  194. 6210 gosub10000:print"try again[146]"
  195. 6240 for i = 1 to6:gosub9000:nexti:gosub11000
  196. 6250 print"":printsp$:printsp$:printsp$
  197. 6260 goto 6050
  198. 6300 return
  199. 7000 rem---- evaluate guess -----------
  200. 7010 rem
  201. 7020 bu = 0: co = 0
  202. 7030 for i = 1 to 4
  203. 7040 for j = 1 to 4
  204. 7050 if i = j then if gs (j) = sn (i) then bu = bu + 1
  205. 7060 if i <> j then if gs (j)= sn (i) then co = co + 1
  206. 7070 next j
  207. 7075 gosub 9000
  208. 7080 next i
  209. 7090 return
  210. 8000 rem---- display guess resultclose-----
  211. 8010 rem
  212. 8020 print"[147]";tab(18);pn$(tn)
  213. 8025 print""spc(15);
  214. 8027 print"guess = ";gs$
  215. 8030 print""spc(17);
  216. 8040 printco;" cow";:if co<>1 then print"s";
  217. 8045 print
  218. 8050 print""spc(17);
  219. 8060 printbu" bull";:if bu<>1 then print"s";
  220. 8065 print
  221. 8068 if bu = 4 then 8200
  222. 8069 if wn$= "y" then 8255
  223. 8070 print"    ";"record your guess and result"
  224. 8080 print"        ** then hit return **":input"";r$
  225. 8090 goto 8500
  226. 8200 print""spc(16);
  227. 8205 wn$= "y"
  228. 8210 print"[212]hat's it[146]"
  229. 8220 if tn = np then 8270
  230. 8230 print"  [204]et the others finish their turn."
  231. 8240 print"            ";:input"** hit return **";r$
  232. 8250 goto 8500
  233. 8255 if tn <> np then 8070
  234. 8260 print"           [212]here was a winner"
  235. 8270 print"